package com.atguigu.gmall.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class CookieUtil {
    /**
     *
     */
    public static String getCookieValue(HttpServletRequest request,String cookieName,boolean isDecoder){
        String str = "";
        Cookie[] cookies = request.getCookies();
        if(cookies == null || StringUtils.isBlank(cookieName)){
            return null;
        }
        try {
            for (Cookie cookie : cookies) {
                if(cookie.getName().equals(cookieName)){
                    if(isDecoder){//如果涉及中文
                           str =  URLDecoder.decode(cookie.getValue(),"UTF-8");
                    }else {
                        str = cookie.getValue();
                    }
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    public static void setCookie(HttpServletRequest request, HttpServletResponse response,
                                 String cartListCookie, String toJSONString,int cookieMaxge,boolean isEncode) {
       try{
            if(toJSONString == null){
                toJSONString = "";
            }else if (isEncode){
                    toJSONString = URLEncoder.encode(toJSONString,"utf-8");
            }
            Cookie cookie = new Cookie(cartListCookie,toJSONString);
            if(cookieMaxge >=0){
                cookie.setMaxAge(cookieMaxge);
            }
            if(null != request) //设置域名的cookie
                cookie.setDomain(getDomianName(request));
            //在域名的根路径下保存
           cookie.setPath("/");
           response.addCookie(cookie);
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       }
    }

    private static String getDomianName(HttpServletRequest request) {
        String domainName = null;
        String serverName = request.getRequestURL().toString();//获取地址栏的url
        if(StringUtils.isBlank(serverName)){
            domainName = "";
        }else {
            serverName = serverName.toLowerCase();
            serverName = serverName.substring(7);
            final int end = serverName.indexOf("/");
            serverName = serverName.substring(0,end);
            String[] split = serverName.split("\\.");
            int length = split.length;
            if(length>3){
                ///www.xxx.com.cn
                domainName = split[length -3 ] + "." + split[length -2] +  "." + split[length -1];
            }else if(length <= 3 && length >1){
                //xxx.com or xxx.cn
                domainName = split[length -2] + "." + split[length -1];
            }else {
                domainName = serverName;
            }
        }
        if(domainName != null && domainName.indexOf(":")>0){
            String[] split = domainName.split("\\:");
            domainName = split[0];
        }
        System.err.println("domainName = " + domainName);

        return domainName;
    }
}
